using System.Threading.Tasks;
using CoreApp.DBAccess;
using CoreApp.DBModel;
using Microsoft.Extensions.Options;
using Dapper;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System;
using CoreApp.Utility;

namespace CoreApp.DapperAccess
{
    public class CommentRepository : RepositoryBase<CommentInfo>, ICommentRepository
    {
        public CommentRepository(IOptions<DBConnectionConfig> option) : base(option)
        {
        }

        public async Task<int> InsertComment(CommentInfo entity)
        {
            try
            {
                string sql = @"INSERT INTO commentInfoinfo (parentid,userid,articleid, Contents,Addtime) VALUES(@parentid,@userid,@articleid, @Contents,@AddTime)";
                return await Insert(entity, sql); 
            }
            catch(Exception ex)
            {
                NLogHelper.Error(ex);
                return 0;
            }            
        }

        public async Task<int> DeleteCommentById(int Id)
        {
            try
            {
                string sql = @"DELETE FROM commentinfo WHERE ID=@Id";
                return await Delete(Id, sql);
            }
            catch(Exception ex)
            {
                NLogHelper.Error(ex);
                return 0;
            }            
        }

        public async Task<int> UpdateComment(CommentInfo entity)
        {
            try
            {
                string sql = @"UPDATE commentinfo SET Contents=@Contents WHERE ID=@ID";
                return await Update(entity, sql);
            }
            catch(Exception ex)
            {
                NLogHelper.Error(ex);
                return 0;
            }            
        }

        public async Task<CommentInfo> QueryComment(int Id)
        {
            try
            {
                string sql = @"SELECT * FROM commentinfo where ID=@Id";
                using (conn)
                {
                    return await conn.QueryFirstOrDefaultAsync<CommentInfo>(sql, new { Id = Id });
                }
            }
            catch(Exception ex)
            {
                NLogHelper.Error(ex);
                return null;
            }            
        }

        public async Task<List<CommentInfo>> GetListByParams(Dictionary<string, string> dic)
        {
            try
            {
                StringBuilder sql=new StringBuilder();
                sql.Append("SELECT * FROM commentinfo where 1=1 ");

                DynamicParameters dp = new DynamicParameters();
                if (dic.Count() > 0)
                {
                    foreach (var item in dic)
                    {
                        switch (item.Key.Trim().ToLower())
                        {
                            case "contents":
                                sql.Append(" and contents like @contents");
                                dp.Add("@contents", "%"+item.Value.Trim()+"%", DbType.String);                            
                                break;
                        }
                    }
                }
                using (conn)
                {
                    return await Task.Run(() => conn.Query<CommentInfo>(sql.ToString(),dp).ToList());
                }
            }
            catch(Exception ex)
            {
                NLogHelper.Error(ex);
                return null;
            }            
        }
    }
}